/*
 * Copyright (c) 2020-2022 Jinsp-tech. All rights reserved.
 */

package com.jinsp.networklibrary;

import android.os.Parcel;
import android.os.Parcelable;

/**
 * Created by luoshuai on 1/13/22
 * <p>
 * 连接配置
 */
public class TCPConnectConfig implements Parcelable {

    private String serviceIP;       //服务端IP
    private int servicePort;        //服务端端口号
    private int connectTimeOut;     //连接超时时间，秒
    private int readIdleTime;       //没有接收数据后多久进入空闲状态，秒
    private boolean isUseHeartBeat; //是否使用心跳
    private int heartBeatInterval;  //心跳间隔时间，秒
    private int heartBeatTimeOut;   //心跳超时时间，秒
    private String codeFormat;      //编码格式
    private String cipher;          //加解密的密码
    private boolean isJsonData;     //是否为Json数据
    private int heartBeatCommandNumber; //心跳命令号
    private int clientStateData;    //客户端发送数据state值
    private boolean isSendDataForJsonString; //发送的泛型数据是否为Json字符串
    private int decoderMaxLineLength;   //最大行解码长度

    private TCPConnectConfig() {
    }

    public String getServiceIP() {
        return serviceIP;
    }

    public int getServicePort() {
        return servicePort;
    }

    public int getConnectTimeOut() {
        return connectTimeOut;
    }

    public int getReadIdleTime() {
        return readIdleTime;
    }

    public boolean isUseHeartBeat() {
        return isUseHeartBeat;
    }

    public int getHeartBeatInterval() {
        return heartBeatInterval;
    }

    public int getHeartBeatTimeOut() {
        return heartBeatTimeOut;
    }

    public String getCodeFormat() {
        return codeFormat;
    }

    public String getCipher() {
        return cipher;
    }

    public boolean isJsonData() {
        return isJsonData;
    }

    public int getHeartBeatCommandNumber() {
        return heartBeatCommandNumber;
    }

    public int getClientStateData() {
        return clientStateData;
    }

    public boolean isSendDataForJsonString() {
        return isSendDataForJsonString;
    }

    public int getDecoderMaxLineLength() {
        return decoderMaxLineLength;
    }

    public static class Builder {

        private String serviceIP;
        private int servicePort;
        private int connectTimeOut = 3;
        private int readIdleTime = 10;
        private boolean isUseHeartBeat;
        private int heartBeatInterval = 6;
        private int heartBeatTimeOut = 5;
        private String codeFormat = "utf-8";
        private String cipher;
        private boolean isJsonData;
        private int heartBeatCommandNumber = 1;
        private int clientStateData;
        private boolean isSendDataForJsonString;
        private int decoderMaxLineLength = 1024;

        public Builder setServiceIP(String serviceIP) {
            this.serviceIP = serviceIP;
            return this;
        }

        public Builder setServicePort(int servicePort) {
            this.servicePort = servicePort;
            return this;
        }

        public Builder setConnectTimeOut(int connectTimeOut) {
            this.connectTimeOut = connectTimeOut;
            return this;
        }

        public Builder setReadIdleTime(int readIdleTime) {
            this.readIdleTime = readIdleTime;
            return this;
        }

        public Builder setUseHeartBeat(boolean useHeartBeat) {
            isUseHeartBeat = useHeartBeat;
            return this;
        }

        public Builder setHeartBeatInterval(int heartBeatInterval) {
            this.heartBeatInterval = heartBeatInterval;
            return this;
        }

        public Builder setHeartBeatTimeOut(int heartBeatTimeOut) {
            this.heartBeatTimeOut = heartBeatTimeOut;
            return this;
        }

        public Builder setCodeFormat(String codeFormat) {
            this.codeFormat = codeFormat;
            return this;
        }

        public Builder setCipher(String cipher) {
            this.cipher = cipher;
            return this;
        }

        public Builder setUseJsonData(boolean isJsonData) {
            this.isJsonData = isJsonData;
            return this;
        }

        public Builder setHeartBeatCommandNumber(int heartBeatCommandNumber) {
            this.heartBeatCommandNumber = heartBeatCommandNumber;
            return this;
        }

        public Builder setClientStateData(int clientStateData) {
            this.clientStateData = clientStateData;
            return this;
        }

        public Builder setSendDataForJsonString(boolean sendDataForJsonString) {
            isSendDataForJsonString = sendDataForJsonString;
            return this;
        }

        public Builder setDecoderMaxLineLength(int decoderMaxLineLength) {
            this.decoderMaxLineLength = decoderMaxLineLength;
            return this;
        }

        public TCPConnectConfig build() {
            TCPConnectConfig tcpConnectConfig = new TCPConnectConfig();
            tcpConnectConfig.serviceIP = serviceIP;
            tcpConnectConfig.servicePort = servicePort;
            tcpConnectConfig.connectTimeOut = connectTimeOut;
            tcpConnectConfig.readIdleTime = readIdleTime;
            tcpConnectConfig.isUseHeartBeat = isUseHeartBeat;
            tcpConnectConfig.heartBeatInterval = heartBeatInterval;
            tcpConnectConfig.heartBeatTimeOut = heartBeatTimeOut;
            tcpConnectConfig.codeFormat = codeFormat;
            tcpConnectConfig.cipher = cipher;
            tcpConnectConfig.isJsonData = isJsonData;
            tcpConnectConfig.heartBeatCommandNumber = heartBeatCommandNumber;
            tcpConnectConfig.clientStateData = clientStateData;
            tcpConnectConfig.isSendDataForJsonString = isSendDataForJsonString;
            tcpConnectConfig.decoderMaxLineLength = decoderMaxLineLength;
            return tcpConnectConfig;
        }
    }

    @Override
    public String toString() {
        return "TCPConnectConfig{" +
                "serviceIP='" + serviceIP + '\'' +
                ", servicePort=" + servicePort +
                ", connectTimeOut=" + connectTimeOut +
                ", readIdleTime=" + readIdleTime +
                ", isUseHeartBeat=" + isUseHeartBeat +
                ", heartBeatInterval=" + heartBeatInterval +
                ", heartBeatTimeOut=" + heartBeatTimeOut +
                ", codeFormat='" + codeFormat + '\'' +
                ", cipher='" + cipher + '\'' +
                ", isJsonData=" + isJsonData +
                ", isUseJsonStringData=" + isSendDataForJsonString +
                ", decoderMaxLineLength=" + decoderMaxLineLength +
                '}';
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(this.serviceIP);
        dest.writeInt(this.servicePort);
        dest.writeInt(this.connectTimeOut);
        dest.writeInt(this.readIdleTime);
        dest.writeByte(this.isUseHeartBeat ? (byte) 1 : (byte) 0);
        dest.writeInt(this.heartBeatInterval);
        dest.writeInt(this.heartBeatTimeOut);
        dest.writeString(this.codeFormat);
        dest.writeString(this.cipher);
        dest.writeByte(this.isJsonData ? (byte) 1 : (byte) 0);
        dest.writeInt(this.heartBeatCommandNumber);
        dest.writeInt(this.clientStateData);
        dest.writeByte(this.isSendDataForJsonString ? (byte) 1 : (byte) 0);
        dest.writeInt(this.decoderMaxLineLength);
    }

    public void readFromParcel(Parcel source) {
        this.serviceIP = source.readString();
        this.servicePort = source.readInt();
        this.connectTimeOut = source.readInt();
        this.readIdleTime = source.readInt();
        this.isUseHeartBeat = source.readByte() != 0;
        this.heartBeatInterval = source.readInt();
        this.heartBeatTimeOut = source.readInt();
        this.codeFormat = source.readString();
        this.cipher = source.readString();
        this.isJsonData = source.readByte() != 0;
        this.heartBeatCommandNumber = source.readInt();
        this.clientStateData = source.readInt();
        this.isSendDataForJsonString = source.readByte() != 0;
        this.decoderMaxLineLength = source.readInt();
    }

    protected TCPConnectConfig(Parcel in) {
        this.serviceIP = in.readString();
        this.servicePort = in.readInt();
        this.connectTimeOut = in.readInt();
        this.readIdleTime = in.readInt();
        this.isUseHeartBeat = in.readByte() != 0;
        this.heartBeatInterval = in.readInt();
        this.heartBeatTimeOut = in.readInt();
        this.codeFormat = in.readString();
        this.cipher = in.readString();
        this.isJsonData = in.readByte() != 0;
        this.heartBeatCommandNumber = in.readInt();
        this.clientStateData = in.readInt();
        this.isSendDataForJsonString = in.readByte() != 0;
        this.decoderMaxLineLength = in.readInt();
    }

    public static final Parcelable.Creator<TCPConnectConfig> CREATOR = new Parcelable.Creator<TCPConnectConfig>() {
        @Override
        public TCPConnectConfig createFromParcel(Parcel source) {
            return new TCPConnectConfig(source);
        }

        @Override
        public TCPConnectConfig[] newArray(int size) {
            return new TCPConnectConfig[size];
        }
    };
}
